package com.axc.controller;


import com.axc.brilliant.domain.WechatUserDO;
import com.axc.brilliant.service.WechatUserService;
import com.axc.common.utils.R;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class AxcController {



    @Autowired
    private WechatUserService wechatUserService;




    @GetMapping("/time")
    public String getTime() {
        String obj = "{\"exchangeType\":\"YGJCJKW05\",\"exchangeCode\":\"3201142019073000002201529764\",\"requestTime\":\"1564416022\",\"body\":[{\"JCDWRZH\":\"\",\"ZXHCS1\":\"0\",\"PJSD\":\"86\",\"TXCLS\":\"5851\",\"SSDL\":\"沪蓉三桥左\",\"TTRQ\":\"20190730\",\"CJSD\":\"00\",\"TJSC\":\"4\",\"DXKCS\":\"0\",\"CDXH\":\"1\",\"ZXKCS\":\"0\",\"WXXKCS\":\"5851\",\"XXHCS\":\"0\",\"PJPDCD\":\"\",\"LLJLBH\":\"A32011400119073003757\",\"LLFL\":\"1\",\"ZXHCS\":\"0\",\"DWBH\":\"A320114001\"}],\"version\":\"1.0\"}";

        return obj;
    }

    /**
     * @param
     * @return
     */
    @CrossOrigin
    @PostMapping("/test")
    // @RequestMapping(value = "/TelemetryList",method =  RequestMethod.POST)
    @ResponseBody
    public String getTelemetryList(@RequestBody Map<String, Object> map) {
        String obj = "{\"exchangeType\":\"YGJCJKW05\",\"exchangeCode\":\"3201142019073000002201529764\",\"requestTime\":\"1564416022\",\"body\":[{\"JCDWRZH\":\"\",\"ZXHCS1\":\"0\",\"PJSD\":\"86\",\"TXCLS\":\"5851\",\"SSDL\":\"沪蓉三桥左\",\"TTRQ\":\"20190730\",\"CJSD\":\"00\",\"TJSC\":\"4\",\"DXKCS\":\"0\",\"CDXH\":\"1\",\"ZXKCS\":\"0\",\"WXXKCS\":\"5851\",\"XXHCS\":\"0\",\"PJPDCD\":\"\",\"LLJLBH\":\"A32011400119073003757\",\"LLFL\":\"1\",\"ZXHCS\":\"0\",\"DWBH\":\"A320114001\"}],\"version\":\"1.0\"}";
        String val = JSONArray.fromObject(map).toString().toLowerCase();
        return val;
    }

    /*
    获取微信用户信息
     */
    @GetMapping("/GetOppid")
    public   String GetOppid(String JSCODE) {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
        requestUrl = requestUrl.replace("APPID", "**");
        requestUrl = requestUrl.replace("SECRET", "******");
        requestUrl = requestUrl.replace("JSCODE", JSCODE);
        RestTemplate restTemplate = new RestTemplate();
        //进行网络请求,访问url接口
        ResponseEntity<String> responseEntity = restTemplate.exchange(requestUrl, HttpMethod.GET, null, String.class);
        //根据返回值进行后续操作
        if(responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
            String sessionData = responseEntity.getBody();
            return sessionData;
        }
        else {
            return "[]";
        }
    }
    /*
    判断是否登录过
     */
    @GetMapping("/GetIsLogin")
    public   R GetIsLogin(String oppid) {
        WechatUserDO wechatUser=new WechatUserDO();
        wechatUser.setStr2(oppid);
        int total = wechatUserService.total(wechatUser);
        if(total>0) {
            wechatUserService.update(wechatUser);
            return R.ok();
        }
        return R.error();
    }
    /*
    保存微信用户信息
     */
    @CrossOrigin
    @PostMapping("/SavaUserInfo")
    public R SavaUserInfo(@RequestBody Map<String, Object> map) {
        WechatUserDO wechatUser=new WechatUserDO();
        wechatUser.setStr2(map.get("str2").toString());
        wechatUser.setUsername(map.get("username").toString());
        wechatUser.setPhonenumber(map.get("phonenumber").toString());
        wechatUser.setStr1(map.get("str1").toString());

        int total = wechatUserService.total(wechatUser);
        if(total>0) {
            wechatUserService.update(wechatUser);
            return R.ok();
        }
        else {
            if (wechatUserService.save(wechatUser) > 0) {
                return R.ok();
            }
        }
        return R.error();
    }
}
